package com.wudimeng.syll.service.impl;


import com.wudimeng.syll.common.ApiResponse;
import com.wudimeng.syll.constants.Constant;
import com.wudimeng.syll.dao.IWeiTuoXinXiDAO;
import com.wudimeng.syll.dao.IYongHuXinXiDAO;
import com.wudimeng.syll.entity.WeiTuoXinXi;
import com.wudimeng.syll.entity.YongHuXinXi;
import com.wudimeng.syll.param.ClientLoginBody;
import com.wudimeng.syll.service.IYongHuXinXiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class YongHuXinXiService implements IYongHuXinXiService {

    @Autowired
    private IYongHuXinXiDAO yongHuXinXiDAO;

    @Autowired
    private IWeiTuoXinXiDAO weiTuoXinXiDAO;

    @Override
    public ApiResponse selectYongHuXinXi(ClientLoginBody loginBody) {
        String YongHuMing = loginBody.getYongHuMing();
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("YongHuMing",YongHuMing);
        if(YongHuMing.length() > 6){
            YongHuXinXi yongHuXinXi = yongHuXinXiDAO.selectYongHuXinXi(paramMap);
            if(yongHuXinXi == null){
                return ApiResponse.error("未查询到用户");
            }
            if(!yongHuXinXi.getYongHuMiMa().equals(loginBody.getYongHuMiMa())){
                return ApiResponse.error("用户名或密码错误");
            }
            return ApiResponse.instance(yongHuXinXi);
        }else{
            WeiTuoXinXi weiTuoXinXi = weiTuoXinXiDAO.selectWeiTuoXinXi(paramMap);
            if(weiTuoXinXi == null){
                return ApiResponse.error("未查询到用户");
            }
            if(!weiTuoXinXi.getWeiTuoMiMa().equals(loginBody.getYongHuMiMa())){
                return ApiResponse.error("用户名或密码错误");
            }
            return ApiResponse.instance(weiTuoXinXi);
        }
    }

    @Override
    public void addYongHuXinXi(YongHuXinXi yongHuXinXi) {
        yongHuXinXiDAO.addYongHuXinXi(yongHuXinXi);
    }

    @Override
    public void updYongHuXinXi(YongHuXinXi yongHuXinXi) {
        yongHuXinXiDAO.updYongHuXinXi(yongHuXinXi);
    }

    @Override
    public ApiResponse updYongHuXinXiMima(Map<String, String> paramMap) {
        String oldMiMa = paramMap.get("OldMiMa");
        YongHuXinXi yongHuXinXi = yongHuXinXiDAO.selectYongHuXinXi(paramMap);
        if(!yongHuXinXi.getYongHuMiMa().equals(oldMiMa)){
            return ApiResponse.error("旧密码错误");
        }
        try {
            yongHuXinXiDAO.updYongHuXinXiMima(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_UPDATE_FAIL);
        }
        return ApiResponse.success(Constant.COMMON_UPDATE_SUCCESS);
    }

    @Override
    public ApiResponse updWeiTuoMima(Map<String, String> paramMap) {
        String oldMiMa = paramMap.get("OldMiMa");
        WeiTuoXinXi weiTuoXinXi = weiTuoXinXiDAO.selectWeiTuoXinXi(paramMap);
        if(!weiTuoXinXi.getWeiTuoMiMa().equals(oldMiMa)){
            return ApiResponse.error("旧密码错误");
        }
        try {
            weiTuoXinXiDAO.updWeiTuoXinXi(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_UPDATE_FAIL);
        }
        return ApiResponse.success(Constant.COMMON_UPDATE_SUCCESS);
    }

    @Override
    public ApiResponse selectWeiTuoXinXiList(Map<String, String> paramMap) {
        return ApiResponse.instance(weiTuoXinXiDAO.selectWeiTuoXinXiList(paramMap));
    }
}
